package com.ilop.sthome.common.command;

import android.content.Context;
import android.util.Log;

import com.aliyun.alink.linksdk.tmp.device.panel.PanelDevice;
import com.ilop.sthome.common.udp.GatewayUdpUtil;
import com.ilop.sthome.common.udp.UdpCommunicationUtil;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;

/**
 * @author skygge
 * @Date on 2020-02-25.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：发送场景相关指令
 */
public class SendSceneCommand {
    private static final String TAG = SendSceneCommand.class.getSimpleName();
    private Context mContext;
    private SiterCommand mSend;
    private String mIotId;
    private String mDeviceName;

    public SendSceneCommand(Context context, DeviceInfoBean device) {
        this.mContext = context;
        this.mIotId = device.getIotId();
        this.mDeviceName = device.getDeviceName();
        mSend = new SiterCommand(device);
    }

    /**
     * 发送json服务
     * @param groupCode json
     */
    private void sendAction(String groupCode){
        if (GatewayUdpUtil.getInstance().isGatewayIntranet(mDeviceName)){
            Log.i(TAG, "sendCodeUDP: " + groupCode);
            new UdpCommunicationUtil(mDeviceName, groupCode);
        }else {
            Log.i(TAG, "sendCodeTCP: " + groupCode);
            PanelDevice panelDevice = new PanelDevice(mIotId);
            panelDevice.init(mContext, null);
            panelDevice.invokeService(groupCode, (b, o) -> {});
        }
    }

    /**
     * 同步场景
     * @param sceneCRC 所有场景CRC校验码，有空缺补0
     * @return
     */
    public void synScene(String sceneCRC){
        sendAction(mSend.synScene(sceneCRC));
    }

    /**
     * 增加/修改场景
     * @param scene 场景信息
     */
    public void increaseScene(String scene){
        sendAction(mSend.increaseScene(scene));
    }

    /**
     * 删除场景
     * @param sceneId 场景编号
     */
    public void deleteScene(int sceneId){
        sendAction(mSend.deleteScene(sceneId));
    }

    /**
     * 切换场景
     * @param position 切换到哪个
     */
    public void choseScene(int position){
        sendAction(mSend.choseScene(position));
    }

}
